﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Input;
using UmlControlLibrary;

namespace UmlControlLibrary
{
    public class DragThumb : Thumb
    {
        public DragThumb()
        {
            base.DragDelta += new DragDeltaEventHandler(DragThumb_DragDelta);
            base.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(DragThumb_MouseUp);
            base.PreviewMouseDown += new MouseButtonEventHandler(DragThumb_PreviewMouseDown);
        }

        public virtual void DragThumb_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            UmlControl item = this.DataContext as UmlControl;
 //           item.OwnerDiagram.DisableCanvasDrag();
        }

        public virtual void DragThumb_MouseUp(object sender, MouseButtonEventArgs e)
        {
            canDrag = false;
            UmlControl item = this.DataContext as UmlControl;
      //      item.MoveEnd();
        }

        public static bool canDrag = false;

        public virtual void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (canDrag)
            {
                Control item = this.DataContext as Control;
                if (item != null)
                {
                    double deltaX, deltaY;
                    deltaX = Canvas.GetLeft(item);
                    deltaY = Canvas.GetTop(item);
                    double left = Math.Max(deltaX + e.HorizontalChange, 0);
                    double top = Math.Max(deltaY + e.VerticalChange,0);
                    deltaX = left - deltaX;
                    deltaY = top - deltaY;
                    
                    Canvas.SetLeft(item, left);
                    Canvas.SetTop(item, top);
                    //(item as UmlControl).Moved(deltaX, deltaY);
                    //          (item as UmlControl).Moved();
                }
            }
        }
    }
}
